package finedev.android.venue;

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;


public class BasicInfo {
	
	public static final String APP_ID ="276169512492238";
	
	public static Facebook FacebookInstance = null;
	public static AsyncFacebookRunner FacebookRunnerInstance = null;
	public static Boolean FacebookLogin = false;
	public static String FACEBOOK_ACCESS_TOKEN;
	
	public static final int FACEBOOK_REQUEST_CODE = 32665;
	
	public static String FACEBOOK_ID = "";
	public static String FACEBOOK_NAME = "";
	public static String FACEBOOK_EMAIL = "";
	public static String FACEBOOK_PICTURE = "";
	
	public static String USER_KEY = "";
	public static String USER_LEVEL = "";
	public static String USER_INCOME = "";
	
	
	public static void FacebookLogin( final String TAG, final Activity activity )
	{
		if ( BasicInfo.FacebookLogin == true ) {
			BasicInfo.FacebookInstance.setAccessToken( BasicInfo.FACEBOOK_ACCESS_TOKEN );
			checkFacebookId( activity );
		} else {
			BasicInfo.FacebookInstance.authorize( activity, new String[] { "email", "read_friendlists", "read_stream", "publish_stream"},new DialogListener() {
				
				@Override
				public void onComplete(Bundle values) {
					try {
						Bundle parameters = new Bundle();
						parameters.putString("fields", "id,name,email,picture" );
						String resultStr = BasicInfo.FacebookInstance.request("me", parameters );
						JSONObject obj = new JSONObject( resultStr );
						
						BasicInfo.FACEBOOK_ID = obj.getString("id");
						BasicInfo.FACEBOOK_NAME = obj.getString("name");
						BasicInfo.FACEBOOK_EMAIL = obj.getString("email");
						BasicInfo.FACEBOOK_PICTURE = obj.getJSONObject("picture").getJSONObject("data").getString("url");
						
						Log.i(TAG, "user_name:" + BasicInfo.FACEBOOK_NAME );
						Log.i(TAG, "user_id:" + BasicInfo.FACEBOOK_ID );

						BasicInfo.FacebookLogin = true;
						BasicInfo.FACEBOOK_ACCESS_TOKEN = BasicInfo.FacebookInstance.getAccessToken();
					} catch ( Exception ex) {
						ex.printStackTrace();
					}
					
					checkFacebookId( activity );

				}

				@Override
				public void onFacebookError(FacebookError error) {
				}

				@Override
				public void onError(DialogError e) {
				}

				@Override
				public void onCancel() {
				}
			});
		}
	}
	
	static private void checkFacebookId( Activity activity )
	{
		ProgressDialog progressDialog = new ProgressDialog( activity );
		progressDialog.setMessage("Logging in...");
		progressDialog.setCancelable(false);
		
		NetworkManager checkManager = new NetworkManager( activity, progressDialog, "facebook_check" );
		checkManager.execute( "http://www.vennue.net/new/m_login.php", 
				String.valueOf( "facebook" ),
				String.valueOf( BasicInfo.FACEBOOK_ID ) );
	}

}
